package org.ucas.faker.web.fetcher.meta;

import org.ucas.faker.web.fetcher.cond.FetchCond;

public class CondTable extends Table {

  public Table original;

  public FetchCond cond;

  public CondTable(String name, Table original, FetchCond cond) {
    this.name = name;
    this.cond = cond;
    if(original != null) {
      this.clazz = original.clazz;
      this.original = original;
      this.columns = original.columns;
    }
  }

  public CondTable() {
  }

  @Override
  public String sql() {
    if(original == null) return "";
    String res = "( SELECT * FROM " + original.sql();
    if(cond != null) res += " WHERE " + cond.sql();
    res += " )";
    if(name != null) res += " AS "+name;
    return res;
  }
}
